package com.dure.hotmemory.utils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.baidu.mobads.AdView;
import com.baidu.mobads.AdViewListener;
import com.baidu.mobads.InterstitialAd;
import com.baidu.mobads.InterstitialAdListener;
import com.baidu.mobads.SplashAd;
import com.baidu.mobads.SplashAdListener;
import com.dure.hotmemory.R;
import com.socks.library.KLog;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

/**
 * Created by Victor on 2016/5/21.
 */
public class BaiduADUtil {

    public static AdView adView;
    public static InterstitialAd interAd;

    // 横幅广告
    public static void showBannerView(final Context context, RelativeLayout view) {
        String adPlaceId = context.getResources().getString(R.string.baidu_banner_id); // 重要：不填写代码位id不能出广告
        adView = new AdView(context, adPlaceId);

        adView.setListener(new AdViewListener() {
            public void onAdSwitch() {
//                KLog.w("lesleixz", "Banner onAdSwitch");
            }

            @Override
            public void onAdClose(JSONObject jsonObject) {

            }

            public void onAdShow(JSONObject info) {
                MobclickAgent.onEvent(context, "banner_present");
                KLog.w("lesleixz", "Banner onAdShow " + info.toString());
            }

            public void onAdReady(AdView adView) {
                KLog.w("lesleixz", "Banner onAdReady " + adView);
            }

            public void onAdFailed(String reason) {
                KLog.w("lesleixz", "Banner onAdFailed " + reason);
            }

            public void onAdClick(JSONObject info) {
                MobclickAgent.onEvent(context, "banner_click");
                KLog.w("lesleixz", "Banner onAdClick " + info.toString());
            }

            public void onVideoStart() {
                KLog.w("lesleixz", "Banner onVideoStart");
            }

            public void onVideoFinish() {
                KLog.w("lesleixz", "Banner onVideoFinish");
            }
        });

        RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        rllp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//        adView.removeAllViews();
        view.addView(adView, rllp);

    }

    // 插屏广告
    public static void showIntertitialView(final Activity context) {
        String adPlaceId = "2384687";
        interAd = new InterstitialAd(context, adPlaceId);
        interAd.setListener(new InterstitialAdListener() {

            @Override
            public void onAdClick(InterstitialAd arg0) {
                Log.i("lesleixz", "Intertitial onAdClick");
            }

            @Override
            public void onAdDismissed() {
                KLog.i("lesleixz", "Intertitial onAdDismissed");
            }

            @Override
            public void onAdFailed(String arg0) {
                KLog.i("lesleixz", "Intertitial onAdFailed");
            }

            @Override
            public void onAdPresent() {
                KLog.i("lesleixz", "Intertitial onAdPresent");
            }

            @Override
            public void onAdReady() {
                KLog.i("lesleixz", "Intertitial onAdReady");
                interAd.showAd(context);
            }

        });
        interAd.loadAd();

    }

    // 开屏广告
    public static void showSplashAD(final Activity activity, ViewGroup adsParent){
        SplashAdListener listener = new SplashAdListener() {
            @Override
            public void onAdDismissed() {
                KLog.i("lesleixz", "onAdDismissed");
                //点击广告的时候会走dismiss，改变waitingOnRestart状态
                ADUtil.jumpWhenCanClick(activity);
            }

            @Override
            public void onAdFailed(String arg0) {
                KLog.i("lesleixz", "onAdFailed");
                ADUtil.jump(activity);
            }

            @Override
            public void onAdPresent() {
                MobclickAgent.onEvent(activity, "splash_present");
                KLog.i("lesleixz", "onAdPresent");
//                if (adPresentListener != null)
//                    adPresentListener.adPresent();
            }

            @Override
            public void onAdClick() {
                KLog.i("lesleixz", "onAdClick");
                // 设置开屏可接受点击时，该回调可用
                MobclickAgent.onEvent(activity, "splash_click");
            }
        };

        /**
         * 构造函数： new SplashAd(Context context, ViewGroup adsParent,
         * SplashAdListener listener,String adPlaceId, boolean canClick);
         */
        String adPlaceId = activity.getResources().getString(R.string.baidu_splash_id); // 重要：不填写代码位id不能出广告
        new SplashAd(activity, adsParent, listener, adPlaceId, true);
    }



    public static void destroyBannerAD(){
        if (adView != null) {
            // 百度退出
            adView.destroy();
            adView = null;
        }
    }
}
